#include <Windows.h>
#include <tchar.h>
#include <assert.h>

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT IDC_BTNRENAME		= 1000;
	static const UINT IDC_STCDIRPATH	= 1001;
	static const UINT IDC_LSTWNDLIST	= 1003;

	switch (nMsg)
	{
		case WM_CREATE:
		{
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
			UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);
			
			SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_ACCEPTFILES);
			CreateWindow(_T("STATIC"), _T("Drop files to window"), WS_CHILD|WS_VISIBLE|SS_PATHELLIPSIS|SS_CENTERIMAGE|SS_LEFT, 10, 10, 770, 30, hWnd, (HMENU)IDC_STCDIRPATH, NULL, NULL);
			CreateWindowEx(WS_EX_CLIENTEDGE, _T("Listbox"), _T(""), WS_CHILD|WS_VISIBLE|LBS_HASSTRINGS|WS_VSCROLL, 10, 50, 770, 300, hWnd, (HMENU)IDC_LSTWNDLIST, NULL, NULL);

			break;
		}

case WM_DROPFILES:
{
	HDROP hDrop = (HDROP)wParam;
	if (NULL != hDrop)
	{
		UINT nDirCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);		// get file count
		if (0 < nDirCount)
		{
			TCHAR szFilePath[MAX_PATH];
			TCHAR szTemp[MAX_PATH + 32];
	
			for (int ii = 0; ii < nDirCount; ii++)						// get file path
			{
				int nPathLen = DragQueryFile(hDrop, ii, szFilePath, NULL);
				DragQueryFile(hDrop, ii, szFilePath, MAX_PATH);		
			
				DWORD dwAttr = GetFileAttributes(szFilePath);
				if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
				{
					_tcscpy(szTemp, _T("Dir  -- "));
				} else {
					_tcscpy(szTemp, _T("File -- "));
				}
				_tcscat(szTemp, szFilePath);
				SendDlgItemMessage(hWnd, IDC_LSTWNDLIST, LB_ADDSTRING, 0, (LPARAM)szTemp);
			}
		} 
		DragFinish(hDrop);
	}

	break;
}

		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szClsName, szWndCaption, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 
								0, 0, 800, 480, NULL, NULL, hInstance, hInstance);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo0034");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}